/*
package com.koron.system.common.aop;

import com.koron.common.core.config.ThreadLocalContext;
import com.koron.system.common.annotation.Dal;
import com.koron.bean.util.UUIDUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;

*/
/**
 * @ClassName DalAspect
 * @description
 * @Author zhouj
 * @Date 2021/7/22 15:24
 *//*

@Aspect
@Component
public class DalAspect {
    private static final String ID = "id";
    private static final String CREATEDID = "createBy";
    private static final String CREATEDPERSON = "createByName";
    private static final String CREATEDDATE = "createTime";
    private static final String UPDATEDID = "updateBy";
    private static final String UPDATEDPERSON = "updateByName";
    private static final String UPDATEDDATE = "updateTime";
    private static final String ISDELETE = "deleteFlag";

    private static final Logger logger = LoggerFactory.getLogger(DalAspect.class);

    @Pointcut("@annotation(com.koron.system.common.annotation.Dal)")
    public void operationPointCut() {
    }

    @Before("operationPointCut()")
    public void around(JoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        Dal dal = method.getAnnotation(Dal.class);
        if ("CREATE".equals(dal.operation())) {
            beforeInsert(point);
        } else {
            beforeUpdate(point);
        }
    }

    public void beforeInsert(JoinPoint point) {
        Object[] args = point.getArgs();
        if (args != null && args.length > 0) {
            Object argument = args[0];
            BeanWrapper beanWrapper = new BeanWrapperImpl(argument);

            String userId = null;
            String userName = null;
            if (ThreadLocalContext.get() != null) {
                userId = ThreadLocalContext.get().getId();
                userName = ThreadLocalContext.get().getName();
            }

            beanWrapper.setPropertyValue(ID, UUIDUtils.getUuid());
            //设置创建时间和修改时间
            if (beanWrapper.isWritableProperty(CREATEDDATE)) {
                beanWrapper.setPropertyValue(CREATEDDATE, new Date());
            }
            //设置创建人和修改人
            if (beanWrapper.isWritableProperty(CREATEDID) && StringUtils.isNotEmpty(userId)) {
                beanWrapper.setPropertyValue(CREATEDID, userId);
            }
            //设置创建人和修改人
            if (beanWrapper.isWritableProperty(CREATEDPERSON) && StringUtils.isNotEmpty(userId)) {
                beanWrapper.setPropertyValue(CREATEDPERSON, userName);
            }
            //设置是否删除
            if (beanWrapper.isWritableProperty(ISDELETE)) {
                beanWrapper.setPropertyValue(ISDELETE, 0);
            }
            logger.info("After insert = {}", ToStringBuilder.reflectionToString(argument, ToStringStyle.SHORT_PREFIX_STYLE));
        }
    }

    public void beforeUpdate(JoinPoint point) {

        String userId = null;
        String userName = null;
        if (ThreadLocalContext.get() != null) {
            userId = ThreadLocalContext.get().getId();
            userName = ThreadLocalContext.get().getName();
        }

        Object[] args = point.getArgs();
        if (args != null && args.length > 0) {
            Object argument = args[0];
            //如果参数是map类型
            if (argument instanceof Map) {
                Map<String,Object> map = (Map<String,Object>) argument;
                map.put(UPDATEDDATE, new Date());
                map.put(UPDATEDID, userId);
                map.put(UPDATEDPERSON, userName);
            } else {
                BeanWrapper beanWrapper = new BeanWrapperImpl(argument);
                //设置修改时间
                if (beanWrapper.isWritableProperty(UPDATEDDATE)) {
                    beanWrapper.setPropertyValue(UPDATEDDATE, new Date());
                }
                //设置修改人
                if (beanWrapper.isWritableProperty(UPDATEDID) && StringUtils.isNotBlank(userId)) {
                    beanWrapper.setPropertyValue(UPDATEDID, userId);
                }
                //设置修改人
                if (beanWrapper.isWritableProperty(UPDATEDPERSON) && StringUtils.isNotBlank(userName)) {
                    beanWrapper.setPropertyValue(UPDATEDPERSON, userName);
                }
            }
            logger.info("After update = {}", ToStringBuilder.reflectionToString(argument, ToStringStyle.SHORT_PREFIX_STYLE));
        }
    }
}*/
